/**
 * 
 */
package com.calendar.guos.client.panels;

import com.calendar.guos.client.GUoS;
import com.google.gwt.accounts.client.AuthSubStatus;
import com.google.gwt.accounts.client.User;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootLayoutPanel;

/**
 * @author Amara Daal
 *
 */
public class LoginStatusPanel extends FlexTable{
	//URI and attributes of the data to be displayed on the screen
    String[] gCalendarFeed = new String[]{ "Google Calendar",
            "http://www.google.com/calendar/feeds/",
            "gdata-calendar.png",
            "http://code.google.com/apis/calendar/" 
    };

    //The scope of the data we want how specific or non specific info 
    // do we need
    final String scope = gCalendarFeed[1];
	public LoginStatusPanel() {
		super();

	    this.setCellPadding(4);
	    this.setCellSpacing(0);
	      //Check if the user is LOGGED_IN or LOGGED_OUT

	}
	 /**
	   * Creates the loginStatus panel to show the current login status
	   */
	  public void update() {
		  

	      

	      
	      //Create the login status panel
	      //This panel will show the users status if they are logged into google
	      this.insertRow(0);
	      this.addCell(0);
	      this.addCell(0);
	      this.addCell(0);
	      this.addCell(0);
	      
	      //An image to display
	      //at the moment its just google calendar logo
	      Image icon = new Image(gCalendarFeed[2]);
	      //Some kind of message to display to our users
	      Label name = new Label("GUOS mang");
	      
	      //Our actual login status label
	      Label statusLabel = new Label();
	      //Initialize status to unknown 
	      statusLabel.setText("Unkown");
	      
	      // A button to log us in our out depending on current state
	      Button loginLogoutButton = new Button();
	      //Initialize to unknown
	      loginLogoutButton.setText("Undefined");
	      
	      
	      //Check if the user is LOGGED_IN or LOGGED_OUT
	      AuthSubStatus status = User.getStatus(scope);
	      
	      /* If the User is logged in
	       * Set the current status to logged in
	       * Set button option to log out
	       */
	      if (status == AuthSubStatus.LOGGED_IN) {
	    	
	        statusLabel.setText("Logged in");
	        loginLogoutButton.setText("Log out");
	        //GUoS.getGoogleCalendarPanel().setVisible(true);
	        //GUoS.getGoogleCalendarPanel().updateCalendarAfterOperation();
	 
	        //perform logout
	        loginLogoutButton.addClickHandler(new ClickHandler() {
	          public void onClick(ClickEvent event) {
	            User.logout(scope);
	            
		  	      //If the user is now logged out update the calendar
		  	      if(User.getStatus(scope).equals(AuthSubStatus.LOGGED_OUT)){
			            //If the user has logged out update calendar
	
			            GUoS.getGoogleCalendarPanel().updateCalendarAtLogout();
		  	      }
		  	      
	            //refresh();
	          }
	        });
	      } 
	      /*
	       * Else if The User is logged out
	       * set the current status to logged out
	       * set button option to login
	       */
	      else if (status == AuthSubStatus.LOGGED_OUT) {
	    	  
	        statusLabel.setText("Logged out");
	        loginLogoutButton.setText("Log in");
	        //GUoS.getGoogleCalendarPanel().setVisible(false);
	        //GUoS.getGoogleCalendarPanel().updateCalendarAfterOperation();

	        //perform login
	        loginLogoutButton.addClickHandler(new ClickHandler() {
	          public void onClick(ClickEvent event) {
	            User.login(scope);
	            
		  	      //If the user is now logged in update the calendar
		  	      if(User.getStatus(scope).equals(AuthSubStatus.LOGGED_IN)){
			            //If the user has logged in update calendar
	
			            GUoS.getGoogleCalendarPanel().updateCalendarAtLogin();
		  	      }

	            
	          }
	        });
	      }
	      
	      //Initialize the loginStatus panel
	      this.setWidget(0, 0, icon);
	      this.setWidget(0, 1, name);
	      this.setWidget(0, 2, statusLabel);
	      this.setWidget(0, 3, loginLogoutButton);
	    
	  }
	  /**
	   * Refreshes this demo by clearing the contents of the panel
	   * and calling showAuthSubStatus to display the authentication status
	   * for each GData system.
	   */
	  public void refresh() {
	    this.clear();
	    update();
	  }
}
